import typeOfUtils from './type-of-utils.js'

class PriUtils {
    /**
     *
     * @param val
     * @return {string|null}
     */
    static getValListJoin(val = []) {
        if (typeOfUtils.isEmpty(val)) {
            return null
        }
        let join = ''
        val.forEach(i => {
            if (typeOfUtils.isString(i)) {
                join += `'${i}',`
            } else if (typeOfUtils.isNumber(i)) {
                join += `${i},`
            }
        })
        join = join.substring(0, join.length - 1)
        return join
    }
}

export default class SqlUtils {
    constructor() {
        this.sql = ''
    }

    /**
     * 排除值
     * @param field
     * @param val
     * @return {SqlUtils}
     */
    andNotIn(field, val = []) {
        const join = PriUtils.getValListJoin(val)
        if (join === null) {
            return this
        }
        this.sql += ` and ${field} not in (${join})`
        return this
    }

    /**
     * 值在里面
     * @param field
     * @param val
     * @return {SqlUtils}
     */
    andIn(field, val = []) {
        const join = PriUtils.getValListJoin(val)
        if (join === null) {
            return this
        }
        this.sql += ` and ${field} in (${join})`
        return this
    }

    /**
     * 大于等于
     * @param field
     * @param val
     */
    andBigEq(field, val) {
        if (typeOfUtils.isEmpty(val)) {
            return this
        }
        if (typeOfUtils.isString(val)) {
            this.sql += ` and ${field} >= '${val}'`
        } else if (typeOfUtils.isNumber(val)) {
            this.sql += ` and ${field} >= ${val}`
        }
        return this
    }

    /**
     * 小于等于
     * @param field
     * @param val
     * @return {SqlUtils}
     */
    andSmallEq(field, val) {
        if (typeOfUtils.isEmpty(val)) {
            return this
        }
        if (typeOfUtils.isString(val)) {
            this.sql += ` and ${field} <= '${val}'`
        } else if (typeOfUtils.isNumber(val)) {
            this.sql += ` and ${field} <= ${val}`
        }
        return this
    }

    /**
     * 获取生成的sql
     * @return {string}
     */
    getSql() {
        let sql = true
        while (sql) {
            this.sql = this.sql.trim()
            const beginNot = ['and', 'or', 'like', 'not', 'in']
            let count = 0
            beginNot.forEach(i => {
                if (this.sql.startsWith(i)) {
                    this.sql = this.sql.substring(this.sql.indexOf(i) + i.length)
                    this.sql = this.sql.trim()
                } else {
                    count++
                }
            })
            if (beginNot.length === count) {
                sql = false
            }
        }
        this.sql = this.sql.trim()
        return this.sql
    }

    /**
     * 模糊查询值两边
     * @param field
     * @param val
     * @return {SqlUtils}
     */
    andLikeAll(field, val) {
        if (typeOfUtils.isEmpty(val)) {
            return this
        }
        this.sql += ` and ${field} like '%${val}%'`
        return this
    }

    /**
     * 值相等
     * @param field
     * @param val
     * @return {SqlUtils}
     */
    and(field, val) {
        if (typeOfUtils.isEmpty(val)) {
            return this
        }
        if (typeOfUtils.isString(val)) {
            this.sql += ` and ${field} = '${val}'`
        } else if (typeOfUtils.isNumber(val)) {
            this.sql += ` and ${field} = ${val}`
        }
        return this
    }

    andNotNull(field) {
        this.sql += ` and (${field} is not null or ${field} != '')`
        return this
    }
}
